﻿using BusinessLogicLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PresentationLayer
{
    public delegate void TaiDSOnSite(List<OnSite> listos);
    public partial class FormTimKiemOnSite : Form
    {
        public FormTimKiemOnSite()
        {
            InitializeComponent();
        }
        public TaiDSOnSite TaiDSOS { get; set; }
        private void FormTimKiemOnSite_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 12; i++)
            {
                cbbThang.AutoCompleteCustomSource.Add(i.ToString());
                
            }
            for (int i = 2010; i <= 2020; i++)
            {
                cbbNam.AutoCompleteCustomSource.Add(i.ToString());
            }
           
            if (radCongTyDiaDiem.Checked==true)
            {
                lblThang.Visible = false;
                lblNam.Visible = false;
                cbbThang.Visible = false;
                cbbNam.Visible = false;

            }
            else
            {
                lblCongTy.Visible = false;
                lblDiaDiem.Visible = false;

                cbbDiaDiem.Visible = false;
                cbbCongTy.Visible = false;

            }
            cbbThang.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbbThang.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            cbbNam.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbbNam.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            cbbDiaDiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbbDiaDiem.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            cbbCongTy.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbbCongTy.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            var list = new OnSiteManager().LayDSOnSite();
            foreach (var item in list.ObjectList)
            {
                
                cbbCongTy.AutoCompleteCustomSource.Add(item.CongTy);
                cbbDiaDiem.AutoCompleteCustomSource.Add(item.DiaDiem);
            }

            cbbCongTy.DataSource = list.ObjectList;
            cbbCongTy.DisplayMember = "CongTy";
            cbbCongTy.ValueMember = "CongTy";

            cbbDiaDiem.DataSource = list.ObjectList;
            cbbDiaDiem.DisplayMember = "DiaDiem";
            cbbDiaDiem.ValueMember = "DiaDiem";
        }

        private void radThangNam_CheckedChanged(object sender, EventArgs e)
        {
            if (radThangNam.Checked)
            {
                lblThang.Visible = true;
                lblNam.Visible = true;
                cbbThang.Visible = true;
                cbbNam.Visible = true;

                lblCongTy.Visible = false;
                lblDiaDiem.Visible = false;
                cbbDiaDiem.Visible = false;
                cbbCongTy.Visible = false;
            }
            else
            {
                lblThang.Visible = false;
                lblNam.Visible = false;
                cbbThang.Visible = false;
                cbbNam.Visible = false;

                lblCongTy.Visible = true;
                lblDiaDiem.Visible = true;
                cbbDiaDiem.Visible = true;
                cbbCongTy.Visible = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int i;
           
            if (radThangNam.Checked)
            {
                if (!int.TryParse(cbbThang.Text, out i) || !int.TryParse(cbbNam.Text, out i))
                {

                    MessageBox.Show("Tháng hoặc năm tìm kiếm không hợp lệ!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (cbbThang.Text != "" && cbbNam.Text != "")
                    {
                        var ar = new OnSiteManager().TimKiemDSOSTheoThangVaNam(Convert.ToInt32(cbbThang.Text), Convert.ToInt32(cbbNam.Text));
                        if (ar.Success)
                            TaiDSOS(ar.ObjectList);
                        else MessageBox.Show("Không có On-Site nào phù hợp!\n\nChi tiết: " + ar.Exceptions[ar.Exceptions.Count - 1].Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else

                        MessageBox.Show("Chưa nhập tháng năm!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }
            else
            {

                var ars = new OnSiteManager().TimKiemDSOSTheoDiaDiemVaCongTy(cbbCongTy.Text, cbbDiaDiem.Text);
                if (ars.Success)
                    TaiDSOS(ars.ObjectList);
                else MessageBox.Show("Không có On-Site nào phù hợp!\n\nChi tiết: " + ars.Exceptions[ars.Exceptions.Count - 1].Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
